<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Test extends Controller_Index_Base
{
    public function before()
    {
       // die('dsds');
    }
    
    /*public function action_index()
    {
        if (HTTP_Request::POST == $this->request->method()) {
            $email    = $this->request->post('email');
            //$password = $this->request->post('password');
            $url      = $this->request->post('url');
                  
            try {
                $password = ORM::factory('user')->generate_password();
                
                $user = ORM::factory('user')->add_user($email, $password);
                $shop = ORM::factory('shop')->add_shop($url);                                
                $_POST = array();
                 
                $user->add_to_shop($shop); 
                Model_Auth::instance()->login($email, $password, true);
                  
                $shop->set_as_current();                     
                Request::current()->redirect(Helper_Admin::get_url() . '/index/start'); 
            } catch (Model_Exception $e) {
                $message = 'Форма заполнена с ошибками!';
            }
        }
        
        //Model_Auth::instance();
                
        $content = new View('index/index/index_tpl');        
        $this->print_template('', $content); 
    }*/
    
    public function action_test()
    {
        die();
        set_time_limit(0);
        $shop_id = $this->request->param('id') ? $this->request->param('id') : null;
        if (!$shop_id) {
            die();    
        }
        $shop = ORM::factory('shop', $shop_id);
        $shop->set_as_current();
        Model_ProductParser_YML::instance()->parse($shop);
    }
    
    public function action_test1()
    {
        set_time_limit(0);

        function get_mime_type(&$structure) { 
    $primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); 
    if($structure->subtype) { 
        return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype; 
    } 
    return "TEXT/PLAIN"; 
} 

function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) { 

    if (!$structure) { 
        $structure = imap_fetchstructure($stream, $msg_number); 
    } 

    if ($structure) {
        if (!$part_number) { 
            $part_number = "1"; 
        } 
     
        if ($mime_type == get_mime_type($structure)) {            
            $text = imap_fetchbody($stream, $msg_number, $part_number); 
            if (!$text) {
                $text = imap_fetchbody($stream, $msg_number, '2');    
            }
            if($structure->encoding == 3) { 
               return imap_base64($text); 
            } else if($structure->encoding == 4) {
               return imap_qprint($text); 
            } else { 
               return $text; 
            } 
        } 
    
        if ($structure->type == 1) /* multipart */ { 
            while(list($index, $sub_structure) = each($structure->parts)) { 
                if ($part_number) { 
                    $prefix = $part_number . '.'; 
                }
                $data = get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1)); 
                if ($data) { 
                    return $data; 
                } 
                $data = get_part($stream, $msg_number, $mime_type, $sub_structure, 1); 
                if ($data) { 
                    return $data; 
                } 
            } // end while 
        } // end multipart 
    } // end structure 
    return false; 
}
        
        $mailboxes = ORM::factory('mailbox')
                ->find_all()
                ->as_array();
foreach ($mailboxes as $mailbox) {
    if (!$mailbox->mailbox_redirect) {
        continue;    
    }
    
    /* try to connect */
    $inbox = imap_open('{teamdesk.ru:143}INBOX', $mailbox->mailbox_redirect, $mailbox->clean_password) or die('Cannot connect: ' . imap_last_error());
    /* grab emails */
    $emails = imap_search($inbox, 'ALL');

    /* if emails are returned, cycle through each... */
    if ($emails) {
        /* for every email... */
        foreach($emails as $id) {
                if ($id <= $mailbox->current_mail_id) {
                    continue;    
                }
            
                /* get information specific to this email */
                $structure = imap_fetchstructure($inbox, $id);
                $overview  = imap_fetch_overview($inbox, $id, 0);
                
                $from          = imap_utf8($overview[0]->from);
                $email         = Model_Main::cut_str('<', '>', $from);
                $client_name   = trim($from);

                // TODO Re
                $subject       = imap_utf8($overview[0]->subject);
                $isset_re      = false;
                if (strstr($subject, 'Re:') !== false) {
                    $isset_re = true;
                    $subject  = trim(str_replace('Re:', '', $subject));     
                }
                
                $date          = imap_utf8($overview[0]->date);
                $date          = strtotime($date);
                
                $message   = get_part($inbox, $id, "TEXT/HTML");
                if (!$message) {
                    $message = get_part($inbox, $id, "TEXT/PLAIN"); 
                    $message = nl2br($message);  
                } 
                
                $ticket = ORM::factory('ticket')->get_ticket_by_email_and_subject($email, $subject);
                // вставка
                if ($isset_re && $ticket->loaded()) {    // ответ
                    $reply = ORM::factory('reply')->add_reply($ticket, $message, $client_name, $email, $date);      
                } else {            // новый тикет
                    $ticket = ORM::factory('ticket')->add_ticket($mailbox, Model_Ticket::TYPE_EMAIL, $client_name, $email, null, $subject, $message, Model_Ticket::STATUS_ACTIVE, 0, $date);     
                }
                
                // обновление current_mail_id
                $mailbox->current_mail_id = $id;
                $mailbox->save();
        }
    }

    /* close the connection */
    imap_close($inbox);
}

   
    }
    
    public function action_test2()
    {
        die();
        set_time_limit(0);
        $shop = ORM::factory('shop', 27);
        $shop->set_as_current();
        ORM::factory('model')->process_shop2($shop); 
    }
    
	/*public function action_index1()
	{    
        $product = ORM::factory('product')->find(123);        
        

        $category = ORM::factory('category', 3);
        //$category = new Model_Category(1);
        //$category = ORM::factory('category')->find(1);   
        
        //$categories = ORM::factory('category')->get_childs();    
        $categories = $category->get_childs();
        $breadcrumbs = $category->get_breadcrumbs(); 
        
        $products = ORM::factory('product')->get_by_category($category);
        
        
        foreach($breadcrumbs as $category) {
            echo $category->name.' -> ';
        }
        echo '<br />';
        
        foreach($categories as $category) {
            echo $category->name.' - '.$category->get_parent()->id.'<br />';
        }
        
        foreach($products as $product) {
            echo $product->name.'  <img src="'.$product->picture.'" /><br />';   
        }
        
        die();
        		
        //$this->print_template($title, $content, $menu);
	} */
    
    /*public function action_test3() {
        $models = ORM::factory('model')->find_all();
        foreach ($models as $model) {
            $reviews = ORM::factory('review')->get_reviews($model);
            foreach ($reviews as $review) {
                $review->usefulness = $review->get_usefulness();
                $review->save();
                //$model->change_count_stars($review->rating);
            }
        }
    }     */
    
    public function action_test3()
    {
        die();
        //echo Model_Auth::instance()->hash_password('123456');
        
        //ORM::factory('user')->add_user('akazorin@gmail.com', 'abc26101991xyz'); 
        
        /*$user = ORM::factory('user', 1);
        $shop = ORM::factory('shop', 3);
        $user->add_to_shop($shop);*/
        
        //var_dump(Model_Auth::instance()->login('akazorin@gmail.com', 'abc26101991xyz', true));
        //Model_Auth::instance()->logout();
        //Model_Auth::instance()->force_login('akazorin@gmail.com');
        //var_dump(Model_Auth::instance()->get_user()->in_shop(ORM::factory('shop', 3))); 
        //var_dump(Model_Auth::instance()->auto_login());
        //var_dump(Model_Auth::instance()->check_password('abc26101991xyz'));
        //var_dump(Model_Auth::instance()->logged_in(Model_User_Shop::ROLE_ADMINISTRATOR));
        
        /*DB::query(NULL, 'CREATE TABLE `ugol`.`categories_2` (
                            `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                            `name` VARCHAR( 255 ) NOT NULL ,
                            `parent_id` SMALLINT UNSIGNED NOT NULL DEFAULT \'0\',
                         INDEX ( `parent_id` )
                         ) ENGINE = MYISAM'
        )->execute();   */
        
        
    } 
    
    public function action_test4()
    {
		die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->offset(6700)
            ->limit(500)
            ->find_all(); 
        foreach ($models as $model) {
            echo '#' . $model->id . ' - ' . $model->name . ':<br />';
            $urls = Model_ReviewParser_Rozetka::instance()->get_reviews_urls($model);
            if (count($urls)) {
                $model->set_rozetka_urls($urls);
                echo '<br /><br />';      
            }  
        }  
    }
    
   /* public function action_test5()
    {
        set_time_limit(0);
        //$model = ORM::factory('model', 2699);
        //Model_ReviewParser_Rozetka::instance()->parse($model); 
        $models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_Rozetka::instance()->parse($model);
        }
    }       */
    
    public function action_test6()
    {
		die();
		set_time_limit(0);
        $models = ORM::factory('model')
            ->where('market_url', '!=', NULL)
            ->and_where('market_url', '!=', '#')
            ->offset(6900)
            ->limit(300)
            ->find_all();
        foreach ($models as $model) {
            $id = (int)str_replace('http://market.yandex.ru/model-opinions.xml?modelid=', '', $model->market_url);           
            $url = 'http://market.yandex.ru/model.xml?modelid='.$id;
            
            $source = Model_ReviewParser_Abstract::_get_source_code($url);
            $source = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__big"', '</span>', $source);
			/*$img = Model_ReviewParser_Abstract::_cut_str(' href="', '"', $source);
			if (!$img) {
				$img = Model_ReviewParser_Abstract::_cut_str(' src="', '"', $source);
			}*/
			$img = Model_ReviewParser_Abstract::_cut_str(' src="', '"', $source);
			if (!$img) {
				continue;
			}
			
			$dir = APPPATH.'../media/uploads/models/';
			$dir = $dir.sprintf('%02x', $model->id % 0xff);
			if (!is_dir($dir)) {
				mkdir($dir);
			}
			$dir = $dir.sprintf('/%d', $model->id);
			if (!is_dir($dir)) {
				mkdir($dir);
			}
			
			$data = file_get_contents($img);
			$f = fopen($dir.'/main.jpg', 'w+');
			fputs($f, $data);
			fclose($f);
			
            echo $model->market_url.' '.$id.' '.$img.'<br />';   
        }
                                               
    }
    
    public function action_test5()
    {
        die();
        set_time_limit(0);
        $tmp_models = ORM::factory('model')
            ->order_by('id', 'DESC')  
            ->offset(60000)
            ->limit(10000)
            ->find_all();
        $models = array();
        foreach ($tmp_models as $model) {
            $models[$model->id] = $model;    
        }
        foreach ($models as &$model) {
            $doubles = ORM::factory('model')
                        ->where('name', 'LIKE', $model->name)
                        ->and_where('id', '!=', $model->id)
                        ->find_all();
            echo $model->id.' - '.$model->name.':<br />';
            foreach ($doubles as $double) {
                echo '<span style="color: red;">' . $double->name . '</span><br />';
                
                /*DB::delete('reviews')
                    ->where('model_id', '=', $double->id)
                    ->execute();  */
                  
                DB::update('products')
                    ->set(array('model_id' => $model->id))
                    ->where('model_id', '=', $double->id)
                    ->execute();    
                    
                unset($models[$double->id]);
                $double->delete();   
            } 
        }
    }   
    
    public function action_test7()
    {
        die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            echo '#' . $model->id . ' - ' . $model->name . ':<br />';
            $urls = Model_ReviewParser_Otzovik::instance()->get_reviews_urls($model);
            if (count($urls)) {
                $model->set_otzovik_urls($urls);
                echo '<br /><br />';      
            }  
        }  
    }
    
    public function action_test8()
    {
        die();
        set_time_limit(0);
        //$model = ORM::factory('model', 2707);
        $model = ORM::factory('model', 580);  
        Model_ReviewParser_Otzovik::instance()->parse($model); 
        /*$models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_Rozetka::instance()->parse($model);
        }  */
    }
    
    // Модели с м
    public function action_test9()
    {  
        die();
        set_time_limit(0);     
        $category_id  = 222;
        $category_url = 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=70-CAT_ID=1620138-EXC=1-PG=10&hid=91610';
		
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('class="b-pager__next" href="', '"', $data)) {
                return 'http://market.yandex.ru'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str('class="b-offers b-offers_type_guru"', 'class="b-model-actioins__shoplist g-none"', $data)) {
                return $_data;
            } 
            return null;
        } 
        
        function get_model_link($data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str('class="b-offers__name" href="', '&amp;', $data)) {
                return 'http://market.yandex.ru'.$link;
            } 
			if ($link = Model_ReviewParser_Abstract::_cut_str('href="/model.xml?', '"', $data)) {
                return 'http://market.yandex.ru/model.xml?'.$link;
            } 
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str('<h3 class="b-offers__title">', '</a>', $data)) {
                return strip_tags($name);
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str('class="b-offers__img" src="', '"', $data)) {
                return $image;
            }
			if ($image = Model_ReviewParser_Abstract::_cut_str('src="http://mdata.yandex.net/i', '"', $data)) {
                return 'http://mdata.yandex.net/i'.$image;
            }			
            return null;   
        }
        
        function get_model_count_prices($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str('Цены</a>', '</li', $data)) {
                if ($count = Model_ReviewParser_Abstract::_cut_str('class="b-model-actioins__cnt">', '</span', $count)) {
                    return (int)$count;
                } 
            } 
            return 0;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str('Отзывы</a>', '</li', $data)) {
                if ($count = Model_ReviewParser_Abstract::_cut_str('class="b-model-actioins__cnt">', '</span', $count)) {
                    return (int)$count;
                } 
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);   
                $count_prices   = get_model_count_prices($model_data);   
                $count_reviews  = get_model_count_reviews($model_data);
				DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_prices', 'count_reviews'))
                    ->values(array($category_id, $name, $link, $image, $count_prices, $count_reviews))->execute();			  
                //echo  $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_prices . ' - ' . $count_reviews . '<br />';
                    
            }
			echo $category_url . '<br />';

			usleep(700000);
            //sleep(1);

            /*$i++;
            if ($i > 5) die();   */
            
        } while ($category_url = get_next_page_url($data));
        
    }
    
    public function action_test10()
    {
        die();
        set_time_limit(0);
        $file = fopen('models123.txt', 'w+');
        $i = 0;
        while (1) {
            $models = DB::select()->from('models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
                fputs($file, $model['id'] . '---' . $model['name'] . "\r\n"); 
            }  
            $i++;
        }
        fclose($file); 
    }
    
    public function action_test11()
    {
        die();
        set_time_limit(0);
        $i = 144;
        while (1) {
            $models = DB::select()->from('tmp_models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
                /*//$name = iconv('utf-8', 'cp1251', $model['name']);
                $name =  $model['goodname'];
                $words = preg_split('/\s/i', $name);
                $name = implode(' ', $words);
                //$name = iconv('cp1251', 'utf-8', $name);  
                echo $name.'<br />';      
                //DB::update('tmp_models')->value('goodname', $name)->where('id', '=', $model['id'])->execute();*/
                
                
                $tmp_name = mb_strtolower(str_replace(array(' ', '-', '(', ')', '.', ',', '+', '/', '&amp;'), '', $model['goodname']));
                //echo $tmp_name.'<br />';
                DB::update('tmp_models')->value('tmp_name', $tmp_name)->where('id', '=', $model['id'])->execute();				
            }  
            $i++;
        }    
    }
    
    public function action_test12()
    {
        die();
        set_time_limit(0); 
        $file = fopen('models123.txt', 'r+');
		$i = 0;
        while ($str = fgets($file)) {
			$i++;
            list($id, $name) = explode('---', $str);
			//if ($id > 154921) {
				//DB::update('tmp_models')->value('goodname', $name)->where('id', '=', $id)->execute();
                DB::update('models')->value('name', trim($name))->where('id', '=', $id)->execute(); 
			//}
        }  
		echo $i;		
    }
	
	public function action_test13()
	{
        die();
		set_time_limit(0); 
        $file = fopen('models_searches.txt', 'r+');
		$i = $j = 0;
        while ($str = fgets($file)) {
			$i++;
            list($name, $searches) = explode("\t", $str);
			$name = str_replace(array(' ', '-', '(', ')', '.', ',', '+', '/', '&amp;'), '', $name);
			$models = DB::select('id', 'tmp_name')->from('tmp_models')->where('tmp_name', 'LIKE', '%'.$name.'%')->and_where('count_searches', 'IS', NULL)->execute()->as_array();
			if (count($models) == 0) {
				$j++;
				continue;
			}
			$model = $models[0];
			DB::update('tmp_models')->value('count_searches', $searches)->where('id', '=', $model['id'])->execute();
        }  
		echo $i . ' - ' . $j;
	}
	
	public function action_test14()
	{
        die();
		set_time_limit(0);
        $i = 144;
        while (1) {
            $models = DB::select()->from('tmp_models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
				$_link = $model['link'].'&';
				if (!($modelid = Model_ReviewParser_Abstract::_cut_str('modelid=', '&', $_link))) {
					die($model['id']);
				}
				$link = 'http://market.yandex.ru/model-opinions.xml?modelid='.$modelid;
                
                DB::update('tmp_models')->value('reviews_link', $link)->where('id', '=', $model['id'])->execute();				
            }  
            $i++;
        }    
	}
	
	public function action_test15()
	{
        die();
		set_time_limit(0);
        $i = 0;
        while (1) {
            $models = DB::select()->from('tmp_models')->where('count_reviews', '>', 0)->or_where('count_searches', '>', 0)->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
				$items = DB::select()->from('models')->where('market_url', '=', $model['reviews_link'])->execute()->as_array();
				if (count($items) == 1) {
					DB::update('models')->value('potential_count_reviews', $model['count_reviews'])
									    ->value('count_searches', $model['count_searches'])->where('id', '=', $items[0]['id'])->execute();	
				} else {
					try { 
						DB::insert('models', array('name', 'potential_count_reviews', 'count_searches', 'market_url'))
							->values(array($model['name'], $model['count_reviews'], $model['count_searches'], $model['reviews_link']))->execute();
					} catch (Database_Exception $e) {
						if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
							continue;
						}
						die($e->getMessage());
					}
				}			
            }  
            $i++;
        }    
	}
	
	public function action_test16()
	{
		die();
        set_time_limit(0);
        $models = ORM::factory('model')
			->where('id', '>', 28001)
			->and_where('id', '<', 61666)
			->and_where('potential_count_reviews', '>', 0)
            ->offset(0)
            ->limit(500)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_YandexMarket::instance()->parse($model);
        }
	}
	
	public function action_test17()
	{
		die();
		$ids = DB::query(Database::SELECT, 'SELECT t1.id FROM yandexmarket_reviews AS t1
							   JOIN yandexmarket_reviews AS t2 ON (t1.model_id = t2.model_id AND t1.source_id = t2.source_id AND t1.id < t2.id)'
        )->execute()->as_array();
		foreach ($ids as $id) {
			DB::delete('yandexmarket_reviews')
                    ->where('id', '=', $id)
                    ->execute();
		}
	}
	
	public function action_test18() {
		die();
		set_time_limit(0);
        $models = ORM::factory('model')
				->offset(60000)
				->limit(5000)
				->find_all();	// where('count_reviews', '=', 0)
		//echo count($models);
		//die();
        foreach ($models as $model) {
            $reviews = DB::select()->from('yandexmarket_reviews')->where('model_id', '=', $model->id)->execute()->as_array();;
            foreach ($reviews as $review) {
                $model->change_count_stars($review['rating']);
            }
			if (count($reviews)) {
				$model->count_reviews = count($reviews);
				$model->save();
			}
        }
		echo '312312';
    }
    
    public function action_test19()
    {
        set_time_limit(0);
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if (!$param) {
            die();    
        }
        list($shop_id, $category_id) = explode('-', $param);
        if (!$shop_id) {
            die();    
        }
        if (!$category_id) {
            $category_id = null;
        }
        $shop = ORM::factory('shop', $shop_id);
        $shop->set_as_current();
        
        $products = ORM::factory('product')->get_products($category_id, 0); 
        $count = $count_found = 0;
        foreach ($products as $product) {
            $count++;
            echo $count.' ('.$product->id.') - '.$product->name;
            
            $models = ORM::factory('model')->search($product->clean_name, 5);
            if ($models) {
                foreach ($models as $model) {
                    //echo '<br />'.$product->get_clean_name().' - '.$model->get_clean_name();
                    echo '<br /><span style="color:green;">'.$model->name.'</span> ('.$model->count_reviews.') <a href="/index/test/to_model/'.$product->id.'-'.$model->id.'" target="_blank">привязать</a>';
                    if ((strstr($product->get_clean_name(), $model->get_clean_name()) !== false ||
                            strstr($model->get_clean_name(), $product->get_clean_name()) !== false)) {
                        $product->to_model($model);
                        $count_found++;
                        echo '<br /><span style="color:red;">В '.$model->name.'</span><br />';
                        break;        
                    }    
                }
                echo '<br /><br />'; 
            }
        }
        echo '<br /><br />Всего - '.count($products).'<br />'.
            'Нашлось наших моделей - '.$count_found.'<br />';
    }
    
    public function action_to_model()
    {
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if ($param) {
            list($product_id, $model_id) = explode('-', $param);
            $product = ORM::factory('product', $product_id);
            $model   = ORM::factory('model', $model_id);
            $product->to_model($model);
        }
    }
    
    // Модели с отзовика
    public function action_test20()
    {  
        set_time_limit(0);     
        $category_id  = 2;
        $category_url = 'http://www.otzovik.com/auto/autospares/tires/';
        
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('<a class=\'npage\' title=\'Следующая\' href=\'', '\'', $data)) {
                return 'http://www.otzovik.com'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str("<td style='padding: 5px' class=imagesel", "</tr>", $data)) {
                return $_data;
            } 
            return null; 
        } 
        
        function get_model_link($data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str("<a href=", ">", $data)) {
                return 'http://www.otzovik.com'.$link;
            }  
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str("<td class=nprods>", "</td>", $data)) {
                return strip_tags($name);
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str(" src=", " width", $data)) {
                if ($image != 'http://i.otzovik.com/nophoto_m.png') {
                    return $image;      
                }
            }           
            return null;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str(">Отзывы:", "</a>", $data)) {
                return (int)trim($count);
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $i++; 
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);    
                $count_reviews  = get_model_count_reviews($model_data);
                //DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_reviews'))
                //    ->values(array($category_id, $name, $link, $image, $count_reviews))->execute();              
                echo  $i . ' - ' . $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_reviews . '<br />';
                    
            }
            echo $category_url . '<br />';

            usleep(700000);
            //sleep(1);
            
        } while ($category_url = get_next_page_url($data));
        
    }
    
    public function action_test23()
    {
        //die();
        set_time_limit(0); 
        $categories = array(1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967,);
        $products = DB::select()
                ->from('tmp_models_3')
                ->where('category_id', 'IN', $categories)
                ->and_where('model_id', 'IS', NULL)
                ->execute()
                ->as_array();
        foreach ($products as $product) {
			try {
				$model = DB::insert('models', array('name', 'tmp_category_id_3', 'potential_count_reviews_3', 'irecommend_url'))
						->values(array($product['name'], $product['category_id'], $product['count_reviews'], $product['link']))->execute();
				DB::update('tmp_models_3')->value('model_id', $model[0])->where('id', '=', $product['id'])->execute();        
			} catch (Database_Exception $e) {
				if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
					continue;
				}
				die($e->getMessage());
			}
        }    
        echo count($products); 
    } 
    
    public function action_test21()
    {	
		//die();
        set_time_limit(0); 
        $categories = array(1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967,); 
        $products = DB::select()
                ->from('tmp_models_3')
                ->where('category_id', 'IN', $categories)
                //->and_where('model_id', 'IS', NULL)
                //->offset(0)
                //->limit(1000)
                ->execute()
                ->as_array(); 
        $count_found = $count = 0;
		
		function _get_clean_name($name) {
			return $name;
			// TODO Если есть слово "для", то оставляем все как есть (???)
			
			$name = _cut_trash($name);
			$words = _get_words($name);
			
			$morphy = Morphy::instance();
			$result = array();
			foreach ($words as $word) {
				$word = mb_strtoupper(trim($word));
				if (!$word) {
					continue;
				}
				
				// Если не нашли, значит либо название товара, либо еще что-то
				// Сохраняем, а русские слова удаляем
				if (!($part_of_speech = $morphy->getPartOfSpeech($word))) {
					$result[] = $word;         
				}   
			}
			
			$result = implode(' ', $result);
			if (!trim($result)) {
				return 'Dfsdfsdw43 324 342';
			}
			return $result;
		}
		
		function _cut_trash($name) {
			// Вырезаем содержимое скобок - (серый)
			// Чаще всего там мусор или цвет
			$name = _cut_brackets($name); 
			
			// TODO Вырезать " и &quot (???)
			//$name = $this->_cut_quotes($name); 
			
			// TODO Удалять все, что справа "+"      
			
			return trim($name);
		}
		
		function _cut_brackets($name, $cut_single_bracket = true) {
			$result = preg_replace('/(\([^\)]+\))/i', '', $name);
			if ($cut_single_bracket) {
				$result = preg_replace('/(\([^\)]+)/i', '', $result);    
			}
			return $result;   
		}
		
		function _get_words($name) {
			$words = explode(' ', $name);
			
			// Удаляем запятые
			foreach ($words as &$word) {
				$word = str_replace(',', '', $word);    
			}
			
			return $words;
		}
		
        foreach ($products as $product) {
            $count++;
            echo $count.' ('.$product['id'].') - '.$product['name'];
            
            $found = false;
            $models = ORM::factory('model')->search($product['name'], 15);
            if ($models) {
                foreach ($models as $model) {
                    //echo '<br />'.$product->get_clean_name().' - '.$model->get_clean_name();
                    echo '<br /><span style="color:green;">'.$model->name.'</span> ('.$model->count_reviews.') <a href="/index/test/test22/'.$product['id'].'-'.$model->id.'" target="_blank">привязать</a>';
					if ((strstr(Model_Main::get_clean_name(_get_clean_name($product['name'])), $model->get_clean_name()) !== false ||
                            strstr($model->get_clean_name(), Model_Main::get_clean_name(_get_clean_name($product['name']))) !== false)) {
						if (!$model->irecommend_url) {
							$model->irecommend_url = $product['link'];
							$model->potential_count_reviews_3 = $product['count_reviews'];
							$model->save();
						}
						DB::update('tmp_models_3')->value('model_id', $model->id)->where('id', '=', $product['id'])->execute();
                        
                        $count_found++;
                        $found = true;
                        echo '<br /><span style="color:red;">#'.$model->id.' '.$model->name.'</span>';
                        break;        
                    }    
                } 
            }
            echo '<br /><br />';
        }
        echo '<br /><br />Всего - '.count($products).'<br />'.
            'Нашлось наших моделей - '.$count_found.'<br />';
    }
    
    public function action_test22()
    {
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if ($param) {
            list($product_id, $model_id) = explode('-', $param);
            $product = DB::select()->from('tmp_models_3')->where('id', '=', $product_id)->execute()->as_array();
            $model   = ORM::factory('model', $model_id);
            $product = $product[0];
 
            $model->irecommend_url = $product['link'];
            $model->potential_count_reviews_3 = $product['count_reviews'];
            $model->save();
            DB::update('tmp_models_3')->value('model_id', $model->id)->where('id', '=', $product['id'])->execute();   
        }
    }    
	
	// Модели с айрекомменд
    public function action_test24()
    {  
        set_time_limit(0);     
        $category_id  = 4;
        $category_url = 'http://www.irecommend.ru/category/konditsionery?tid=77028';
        
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('class="pager-next"><a href="', '"', $data)) {
				$url = str_replace('&amp;', '&', $url);
                return 'http://www.irecommend.ru'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str("<div class=\"block\">", "<!-- /end node.tpl.php -->", $data)) {
                return $_data;
            } 
            return null; 
        } 
        
        function get_model_link(&$data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str("class=\"title\"><a href=\"", "\">", $data)) {
                return 'http://www.irecommend.ru'.$link;
            }  
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str("<h2 class=\"title\">", "</h2", $data)) {
                return trim(strip_tags($name));
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str(" src=\"", "\"", $data)) {
                //if ($image != 'http://i.otzovik.com/nophoto_m.png') {
                    return $image;      
                //}
            }           
            return null;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str("\">Читать отзывы (", ")</a", $data)) {
                return (int)trim($count);
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $i++; 
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);    
                $count_reviews  = get_model_count_reviews($model_data);
                //DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_reviews'))
                //    ->values(array($category_id, $name, $link, $image, $count_reviews))->execute();              
                echo  $i . ' - ' . $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_reviews . '<br />';
                    
            }
            echo $category_url . '<br />';

            usleep(700000);
            //sleep(1);
            
        } while ($category_url = get_next_page_url($data));
        
    }
	
	public function action_test25()
    {
        die();
        set_time_limit(0); 
        $models = DB::select()
                ->from('models')
				->offset(100000)
                ->limit(100000)
                ->execute()
                ->as_array();
        foreach ($models as $model) {
			if ($model['market_url']) {
				$products = DB::select()
					->from('tmp_models')
					->where('reviews_link', '=', $model['market_url'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('yandexmarket_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
			if ($model['otzovik_urls']) {
				$products = DB::select()
					->from('tmp_models_2')
					->where('model_id', '=', $model['id'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('otzovik_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
			if ($model['irecommend_url']) {
				$products = DB::select()
					->from('tmp_models_3')
					->where('link', '=', $model['irecommend_url'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('irecommend_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
        }    
        echo count($models); 
    } 
	
	public function action_test26()
    {
        $text = '<div class="js-helper js-readmsg-msg">
    <style type="text/css"></style>
     <div>
        <base target="_self" href="https://e.mail.ru/">
        
            <div id="style_14236817660000000508_BODY">И нам не стыдно совсем<img src="https://proxy.imgsmail.ru?h=nL1k5iO5z2irYgx-lrjIaQ&amp;e=1423940997&amp;url171=bWFuZHJpbGxhcHAuY29tL3RyYWNrL29wZW4ucGhwP3U9MzA0NzM2ODcmaWQ9N2E1M2U4NjYxZWM3NDAwNTkyOWUwMDkwMjExM2Q4YTM~" height="1" width="1">
</div>
            
        
        <base target="_self" href="https://e.mail.ru/">
    </div>

    
</div>';
        
        echo Helper_Admin::prepare_text($text, 'base');
    }
} 
